package test.my.com.myapplicationtest.SocketClient;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import test.my.com.myapplicationtest.utils.LogUtil;

/**
 * Author : Cooper
 * Time : 2017/10/30  15:48
 * Description : 远程绘画view
 */

public class RemoteDrawView extends View {

    private Paint mPaint;
    private Path mLinePath;
    private float drawX;
    private float drawY;

    private ActionPosition mActionPosition = new ActionPosition();
    private ActionPositionCallBack  mActionPositionCallBack;

    public void setActionPositionCallBack(ActionPositionCallBack actionPositionCallBack) {
        mActionPositionCallBack = actionPositionCallBack;
    }

    public RemoteDrawView(Context context) {
        super(context);
        init();
    }

    public RemoteDrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RemoteDrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(10);
        mLinePath = new Path();
        mActionPosition.from = 0;
        mActionPosition.to = 1;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawPath(mLinePath,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogUtil.d("ah  onTouchEvent +++ === ");
        drawX = event.getX();
        drawY = event.getY();
        mActionPosition.positionX = event.getX();
        mActionPosition.positionY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mActionPosition.actionType = ActionPosition.ACTION_DOWN;
                mLinePath.moveTo(drawX,drawY);
                LogUtil.d("ah  ACTION_DOWN  downX === "+drawX);
                break;
            case MotionEvent.ACTION_MOVE:
                mActionPosition.actionType = ActionPosition.ACTION_MOVE;
                mLinePath.lineTo(drawX,drawY);
                LogUtil.d("ah  ACTION_MOVE  downX === "+drawX);
                break;
            case MotionEvent.ACTION_UP:
                mActionPosition.actionType = ActionPosition.ACTION_UP;
                LogUtil.d("ah  ACTION_UP  ");
                break;
        }
        invalidate();
        if (mActionPositionCallBack != null) {
            mActionPositionCallBack.getActionPosition(mActionPosition);
        }
        return true;
    }

    /**
     * 利用远程数据绘制线
     * @param actionPosition
     */
    public void remoteDraw(ActionPosition actionPosition) {
        switch (actionPosition.actionType) {
            case ActionPosition.ACTION_DOWN:
                mLinePath.moveTo(actionPosition.positionX,actionPosition.positionY);
                break;
            case ActionPosition.ACTION_MOVE:
                mLinePath.lineTo(actionPosition.positionX,actionPosition.positionY);
                break;
            case ActionPosition.ACTION_UP:
                break;
        }
        invalidate();
    }

    interface ActionPositionCallBack{
        void getActionPosition(ActionPosition actionPosition);
    }

}
